﻿//Copyright Notice:  ©2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.Text;

namespace Microsoft.InformationSecurity.CISF.Security.AuthZServices
{
    /// <summary>
    /// Gets all the users from the cache that have the specified role.
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class AuthZService : IAuthZService
    {
        #region Private Variables
        string role = "";
        string alias = "";

        #endregion

        #region Methods

        #region GetUserInformation
        /// <summary>
        /// Get the user information from the cache.
        /// </summary>
        /// <param name="alias">The alias of the user to get User data for.</param>
        /// <returns>A User object.</returns>
        public User GetUserInformation(string name)
        {
            try
            {
                this.alias = (AuthZGlobals.ParseName(name))[(int)AuthZGlobals.NamePart.sAMAccountName];

                GetUsers users = new GetUsers(alias, GetUsers.QueryType.Alias);

                if (users == null)
                    return null;

                if (users.Users.Count > 0)
                    return users.Users[0];
                else
                    return null;
            }

            #region Catch
            catch (Exception e)
            {
                int myEventID = 10;
                short myCategory = 10;
                // Write an informational entry to the event log.
                StringBuilder msg = new StringBuilder("Error in AuthZService:  ");
                msg.Append(e.Message);
                if (e.InnerException != null)
                {
                    msg.Append("\nInnerException:  ");
                    msg.Append(e.InnerException.Message);
                }
                if (e.StackTrace != null)
                {
                    msg.Append("\nStack Trace:  ");
                    msg.Append(e.StackTrace);
                }

                EventLog.WriteEntry("AuthZService", msg.ToString(), EventLogEntryType.Error, myEventID, myCategory);
                throw e;
            }

            #endregion

        }

        #endregion

        #region GetUsersInRole
        /// <summary>
        /// Gets all the users from the cache that have the specified role.
        /// </summary>
        /// <param name="role">The name of the role.</param>
        /// <returns>Collection of User objects.</returns>
        public Collection<User> GetUsersInRole(string role)
        {
            try
            {
                this.role = role;

                GetUsers users = new GetUsers(role, GetUsers.QueryType.Role);

                if (users == null)
                    return null;

                if (users.Users.Count > 0)
                    return users.Users;
                else
                    return null;
            }

            #region Catch
            catch (Exception e)
            {
                int myEventID = 10;
                short myCategory = 10;
                // Write an informational entry to the event log.
                StringBuilder msg = new StringBuilder("Error in AuthZService:  ");
                msg.Append(e.Message);
                if (e.InnerException != null)
                {
                    msg.Append("\nInnerException:  ");
                    msg.Append(e.InnerException.Message);
                }
                if (e.StackTrace != null)
                {
                    msg.Append("\nStack Trace:  ");
                    msg.Append(e.StackTrace);
                }

                EventLog.WriteEntry("AuthZService", msg.ToString(), EventLogEntryType.Error, myEventID, myCategory);
                throw e;
            }

            #endregion

        }

        #endregion

        #endregion
    }
}
